// 购物车
define(()=>{
class shopping {
    constructor() {
        this.list = JSON.parse(localStorage.getItem("cart")) || []
        this.father = document.querySelector(".father")
        this.gologon = document.querySelector(".gologon")
        this.tbody = document.querySelectorAll("tbody")
        this.selectAll = document.querySelectorAll(".Allcheck")
        this.Andnum = document.querySelector(".Andnumn")
        this.Andprice = document.querySelector(".Andprice")
        this.init()
    }
    // 页面初始化
    init() {
        this.render()
        this.Btn()
        this.count()
    }
    // 数据渲染
    render() {

        // 判断有没有登录
        // 没有
        if (getCookie("username") == undefined) {
            this.selectAll.forEach((item)=>{
                item.disabled=true
            })
            this.gologon.style.display = "flex"
            this.tbody[0].innerHTML = `
<tr>
<td style="color:red; font-size:30px ;">你还没有登录呢!快去登录</td>
</tr>

`
return
        } else {

            this.gologon.style.display = "none"
            // 判断有没有数据
            // 没有数据
            if (!this.list.length) {
                // 全选禁用
                this.selectAll.forEach((item)=>{
                    item.disabled=true
                })

                $("tbody")[0].innerHTML = `
    <tr>
    <td style="color:red; font-size:30px ;">
    你还没有东西！
    <a href="./paging_list.html">快去购物吧</a>
    </td>
    </tr>

    `
            } else {
                this.tbody[0].innerHTML = "";
                this.list.forEach(item => {

                    this.tbody[0].innerHTML += `
    <tr>
    <td><input type="checkbox" class="seclet"  data-id=${item.goods_id} ${item.is_select && "checked"}  ></td>
    <td>
    <a href="./detailPages.html?id=${item.goods_id}">
    <img src=${item.img_small_logo} alt="">
        <p class="tilie .texthide">${item.title}</p>
        <i>免费分期</i>
        </a>
    </td>
    <td><span class="pirice">￥${item.price}</span></td>
    <td class="pointer">
        <span class="sub" data-id=${item.goods_id}>-</span>
        <input type="text" class="txt" value="${parseFloat(item.cart_number)}" />
        <span class="add"   data-id=${item.goods_id}>+</span>

    </td>
    <td><span class="subTotal">￥${item.price * item.cart_number}</span></td>
    <td><span class="del" data-id=${item.goods_id} style=" cursor: pointer;">删除</span></td>
</tr>




    `

                });


            }


        }
        //数据持久化
        localStorage.setItem('cart', JSON.stringify(this.list))

    }
    // 事件委派
    Btn() {

        this.father.addEventListener("click", (e) => {
            e = e || window.event
            let target = e.target || e.srcElement
            //  全选功能
            if (target.className == "Allcheck") {
                let type = target.checked
                this.selectAll.forEach((item) => {
                    item.checked = type
                })
                //改变数据的状态

                this.list.forEach(item => {
                    item.is_select = type
                })
                //重新渲染数据
                this.render()
                this.count()

            }
            // 单选功能
            if (target.className == "seclet") {
                let id = target.dataset.id - 0
                //   找到这条数据
                let goods = this.list.find((item) => {
                    return item.goods_id == id
                })
                // 改变选择状态
                goods.is_select = !goods.is_select

                this.render()
                this.count()

            }

            // 增加
            if (target.className == "add") {
                let id = target.dataset.id - 0

                //   找到这条数据
                let goods = this.list.find((item) => {
                    return item.goods_id == id
                })
                // 改变选择状态
                if (goods.cart_number == goods.goods_number) {
                    alert('亲，只能买这么多哦！')
                    return false
                }
                goods.cart_number++
                this.render()
                this.count()

            }
            // 减少

            if (target.className == "sub") {
                let id = target.dataset.id - 0

                //   找到这条数据
                let goods = this.list.find((item) => {
                    return item.goods_id == id
                })
                // 改变选择状态
                if (goods.cart_number == 1) {
                    alert('不能再少啦！')
                    return false
                }
                goods.cart_number--
                this.render()
                this.count()

            }
            // 删除
            if (target.className == "del") {
                let id = target.dataset.id - 0
                console.log(id)
                //   找到这条数据
                let goods = this.list.find((item) => {
                    return item.goods_id == id
                })
                if (confirm("确定要删除？")) {
                    let index = this.list.findIndex((item) => {
                        return item.goods_id == id
                    })
                    this.list.splice(index, 1)

                }
                //判断数组是不是为空
                if (!this.list.length) {
                    this.selectAll.forEach((item) => {
                        item.checked = false
                    })
                    console.log(5)

                }
                this.render()
                this.count()


            }
            // // 全删
            if (target.className == "delAll") {


                let res = this.list.filter((item) => {
                    return item.is_select == false
                })
                this.list = res


                if (!this.list.length) {
                    this.selectAll.forEach((item) => {
                        item.checked = false
                    })


                }

                this.render()
                this.count()


            }

        }, false)




    }
    //    计算
    count() {

        let num = 0
        let price = 0
        this.list.forEach((item) => {
            if (item.is_select == true) {
                num += item.cart_number
                price += item.price * item.cart_number

            }
            let res = this.list.every((item) => {
                return item.is_select == true
            })

            if (res) {
                this.selectAll.forEach((item) => {
                    item.checked = true
                })
            } else {
                this.selectAll.forEach((item) => {
                    item.checked = false
                })
            }




        })





        //进行赋值操作
        this.Andnum.innerHTML = num
        this.Andprice.innerHTML = price.toFixed(2)




    }






}
 return new shopping()

})